import { request } from "@/http/axios"
// 删除文件
export function removeFile(fileName) {
  const url = `upload/delFile?fileName=${encodeURIComponent(fileName)}`;
  return request({
    url: url,
    method: 'get',
  });
}

// 删除图片
export function removeImgFile(fileName) {
  const url = `upload/delImgFile?fileName=${encodeURIComponent(fileName)}`;
  return request({
    url: url,
    method: 'get',
  });
}

// 获取文件
export function getFile(fileName) {
  const url = `upload/getFiles?fileName=${encodeURIComponent(fileName)}`;
  return request({
    url: url,
    method: 'get',
  });
}

//上传文件 分片上传 保存文件
export function uploadFileSave(data){
  return request({
    url: '/upload/save',
    method: "post",
    data: data,
    headers: {'Content-Type': 'multipart/form-data'}
  })
}
//上传文件 分片上传 合并文件
export function uploadFileMerge(data){
  return request({
    url: '/upload/combine',
    method: "post",
    data: data,
  })
}
//下载文件
export function FileDownloadFiaName(fileName) {
  const url = `upload/downloadFile?fileName=${encodeURIComponent(fileName)}`;
  return request({
    url: url,
    method: 'get',
    responseType: 'blob',
  })
}
// 获取文件二进制文件
export function getFileBytes(fileName) {
  const url = `upload/getFileContent?fileName=${encodeURIComponent(fileName)}`;
  return request({
    url: url,
    method: 'get',
    responseType: 'arraybuffer',
  })
}

//上传图片
export function uploadImg(data) {
  return request({
    url: '/upload/singleFileUpload',
    method: "post",
    data: data,
    headers: {'Content-Type':'multipart/form-data'}
  })
}

/**
 * @description : 获取随机字符串
 * @param        {Number} length 字符串长度，如不传则默认生成32位
 * @return       {String} str 生成的字符串
 */
export function getRandomString(length = 30) {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  const maxLength = chars.length
  let str = ''
  for (let i = 0; i < length; i++) {
    str = str + chars.charAt(Math.floor(Math.random() * maxLength))
  }
  return str
}
